package com.ikingtech.platform.service.system.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.user.model.*;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.system.user.entity.*;
import com.ikingtech.platform.service.system.user.entity.UserDO;
import com.ikingtech.platform.service.system.user.mapper.UserMapper;
import com.ikingtech.framework.sdk.user.model.UserQueryParamDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @author tie yan
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserRepository extends ServiceImpl<UserMapper, UserDO> {

    public UserDO getByCredential(String credentialName) {
        return this.getOne(Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getUsername, credentialName)
                .or()
                .eq(UserDO::getPhone, credentialName)
                .or()
                .eq(UserDO::getEmail, credentialName)
                .or()
                .eq(UserDO::getIdentityNo, credentialName));
    }

    public UserDO getByUsername(String username) {
        return this.getOne(Wrappers.<UserDO>lambdaQuery().eq(UserDO::getUsername, username));
    }

    public boolean exist(String id) {
        return this.baseMapper.exists(Wrappers.<UserDO>lambdaQuery().eq(UserDO::getId, id));
    }

    public Boolean usernameExist(String username) {
        return this.baseMapper.exists(Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getUsername, username));
    }

    public Boolean usernameExist(String id, String username) {
        return this.baseMapper.exists(Wrappers.<UserDO>lambdaQuery()
                .ne(UserDO::getId, id)
                .eq(UserDO::getUsername, username));
    }

    public Boolean phoneExist(String phone) {
        return this.baseMapper.exists(Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getPhone, phone));
    }

    public Boolean phoneExist(String id, String phone) {
        return this.baseMapper.exists(Wrappers.<UserDO>lambdaQuery()
                .ne(UserDO::getId, id)
                .eq(UserDO::getPhone, phone));
    }

    public PageResult<UserDO> listPage(UserQueryParamDTO queryParam, List<String> userIds) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.<UserDO>lambdaQuery()
                .like(Tools.Str.isNotBlank(queryParam.getName()), UserDO::getName, queryParam.getName())
                .like(Tools.Str.isNotBlank(queryParam.getUsername()), UserDO::getUsername, queryParam.getUsername())
                .like(Tools.Str.isNotBlank(queryParam.getNickname()), UserDO::getNickname, queryParam.getNickname())
                .like(Tools.Str.isNotBlank(queryParam.getPhone()), UserDO::getPhone, queryParam.getPhone())
                .like(Tools.Str.isNotBlank(queryParam.getEmail()), UserDO::getEmail, queryParam.getEmail())
                .eq(null != queryParam.getAdmin(), UserDO::getAdminUser, queryParam.getAdmin())
                .eq(null != queryParam.getLocked(), UserDO::getLocked, queryParam.getLocked())
                .eq(Tools.Str.isNotBlank(queryParam.getSex()), UserDO::getSex, queryParam.getSex())
                .in(Tools.Coll.isNotBlank(userIds), UserDO::getId, userIds)
                .eq(Tools.Str.isNotBlank(queryParam.getLockType()), UserDO::getLockType, queryParam.getLockType())
                .orderByDesc(UserDO::getCreateTime);
        LocalDate createTime = queryParam.getCreateTime();
        if (createTime != null) {
            queryWrapper.between(UserDO::getCreateTime, createTime.atStartOfDay(), createTime.atStartOfDay().plusDays(1).minusSeconds(1));
        }
        LocalDate updateTime = queryParam.getUpdateTime();
        if (updateTime != null) {
            queryWrapper.between(UserDO::getUpdateTime, updateTime.atStartOfDay(), updateTime.atStartOfDay().plusDays(1).minusSeconds(1));
        }
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), queryWrapper));
    }

    public void removeByUsername(String username) {
        this.remove(Wrappers.<UserDO>lambdaQuery().eq(UserDO::getUsername, username));
    }

    public List<UserDO> listByIdsAndName(List<String> ids, String name) {
        return this.list(Wrappers.<UserDO>lambdaQuery()
                .in(Tools.Coll.isNotBlank(ids), UserDO::getId, ids)
                .like(Tools.Str.isNotBlank(name), UserDO::getName, name));
    }

    public List<UserDO> listByPhones(List<String> phones) {
        return this.list(Wrappers.<UserDO>lambdaQuery().in(UserDO::getPhone, phones));
    }

    public PageResult<UserDO> listPageByIds(Integer page, Integer rows, List<String> ids) {
        return PageResult.build(this.page(new Page<>(page, rows), Wrappers.<UserDO>lambdaQuery().in(UserDO::getId, ids)));
    }

    public List<String> listIdByName(String name) {
        return this.listObjs(Wrappers.<UserDO>lambdaQuery().like(UserDO::getName, name), String.class::cast);
    }
}
